package com.gejt.test.controller;

import com.gejt.test.entity.Author;
import com.gejt.test.entity.Post;
import com.gejt.test.common.GraphQLClient;
import com.gejt.test.repositories.AuthorRepository;
import com.gejt.test.repositories.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@Controller
public class IndexController {

    @Autowired
    private PostRepository postRepository;
    @Autowired
    private AuthorRepository authorRepository;

    @Autowired
    private GraphQLClient graphQLClient;

    @RequestMapping(value = "/")
    public @ResponseBody String index(){
        return "ok";
    }

    @RequestMapping(value = "/hello")
    public @ResponseBody String hello(String name){
        return "Hello "+name;
    }

    @QueryMapping(value = "authorList")
    public Iterable<Author> authorList(@Argument Integer pageNum) {
        return authorRepository.findAll();
    }

//    @QueryMapping(value = "recentPosts")
//    public List<Post> recentPosts(@Argument int count, @Argument int offset) {
//        return postRepository.getRecentPosts(count, offset);
//    }
//
//    @SchemaMapping(typeName="Post", field="author")
//    public Author getAuthor(Post post) {
//        return authorRepository.getAuthorById(post.getAuthorId());
//    }
//
//    @MutationMapping
//    public Post createPost(@Argument String title, @Argument String text,
//                           @Argument String category, @Argument String authorId) {
//        Post post = new Post();
//        post.setId(UUID.randomUUID().toString());
//        post.setTitle(title);
//        post.setText(text);
//        post.setCategory(category);
//        post.setAuthorId(authorId);
//        postRepository.savePost(post);
//        return post;
//    }


    @RequestMapping("/executeGraphQLQuery")
    @ResponseBody
    public  String executeGraphQLQuery(String query){
        return graphQLClient.executeGraphQLQuery(query);
    }



}
